package cn.ghx.tool.novel.grabber;


import cn.ghx.tool.novel.domain.Book;
import cn.ghx.tool.novel.domain.Caption;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

public abstract  class GrabberBase implements Grabber{

    String url;
    private String path;


    public GrabberBase(String url,String path) {
        this.url = url;
        this.path = path;
    }

    @Override
    public void parse() {
        try {
            Book book = parseBook();
            long start = System.currentTimeMillis();
            writeBookToFile(book,path);
            long end = System.currentTimeMillis();
            long cost = end - start;
            log(String.format("写入文件，耗时：%s ms,%s s, %s min", cost,cost/1000,cost/1000/60));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    protected  Book parseBook() throws IOException {
        long start = System.currentTimeMillis();

        Document doc = Jsoup.connect(url).get();
        log("开始抓取图书...");
        Book book = this.getBookWithoutContent(doc);
        log(String.format("书名：%s\n作者：%s\n目录,总共【%s】章",
                book.getTitle(),
                book.getAuthor(),
                book.getCaptions().size()));

        for (Caption cap:book.getCaptions()){
            String content = this.getCaptionContent(cap.getLink());
            cap.setContent(content);
        }

        long end = System.currentTimeMillis();
        long cost = end - start;

        log(String.format("抓取结束，耗时：%s ms,%s s, %s min", cost,cost/1000,cost/1000/60));

        return book;
    }

    protected abstract Book getBookWithoutContent(Document doc);
    protected abstract String getCaptionContent(String link);


    protected void writeBookToFile(Book book, String path) throws IOException {
        if(book != null) {
            String filename = book.getTitle() + ".txt";
            path = path + File.separator+filename;
            log("---写入文件:"+path);
            File file = new File(path);
            if(!file.getParentFile().exists()){
                file.getParentFile().mkdirs();
            }
            FileWriter writer = new FileWriter(file);
            writer.write(String.format("%s\n作者：%s\n\n",book.getTitle(),book.getAuthor()));
            for(Caption cp:book.getCaptions()){
                writer.write(String.format("%s\n%s\n\n",cp.getTitle(),cp.getContent() == null ? cp.getLink() : cp.getContent()));
            }
            writer.flush();
            writer.close();
        }
    }

    static void log(String s) {
        System.out.println(s);
    }

    boolean progress(int total,double index,Caption caption){
        String flag;
        if(caption.getContent()!=null){
            flag = "【成功】";
        }else{
            flag = "【失败】";
        }
        log("【"+index+"/"+total+","+(index/total* 100)+"%】"+flag+caption.getTitle()+"\t"+caption.getLink());
        return caption.getContent() != null;
    }
}
